/* linkit.js
 * By François Leporcq
 * MIT Licensed.
 */
var CurveLink = PolyLink.extend({
    _className: "CurveLink",
    init: function(startPolygon, endPolygon, startLabel, endLabel, midLabel, start, end){
        this._start = start != null ? start : new Anchor();
        this._end = end != null ? end : new Anchor();
        this._line = new BezierCurve(this._start, this._end);
        this._startPolygon = startPolygon;
        this._endPolygon = endPolygon;
        this._startLabel = startLabel;
        this._endLabel = endLabel;
        this._midLabel = midLabel;
        this.setShapes(this.getShapes());
        this._updateStartPolygon();
        this._updateEndPolygon();
        this._updateStartLabel();
        this._updateEndLabel();
        this._updateMidLabel();
    },
    _addMidPoints: function(){
        this._removeMidPoints();
        if(ECardinalPoint.areOpposite(this._start.getCardinalPoint(), this._end.getCardinalPoint())){
            var pointAfterStart = this._getMidPoint(this._start, 2);
            var pointBeforeEnd = this._getMidPoint(this._end, 2);
            this._line.setFirstControl(pointAfterStart);
            this._line.setSecondControl(pointBeforeEnd);
        }else{
            var pointAfterStart = this._getMidPoint(this._start, 1);
            this._line.setFirstControl(pointAfterStart);
        }
    },
    setStart: function(start){
        this._start = start;
        this._line.setStart(this._start);
        this._addMidPoints();
        this._updateStartPolygon();
        this._updateEndPolygon();
        this._updateStartLabel();
        this._updateMidLabel();
    },
    setEnd: function(end){
        this._end = end;
        this._line.setEnd(this._end);
        this._addMidPoints();
        this._updateStartPolygon();
        this._updateEndPolygon();
        this._updateEndLabel();
        this._updateMidLabel();
    },
    _removeMidPoints: function(){
        this._line.setFirstControl(new Point());
        this._line.setSecondControl(null);
        this._updateStartPolygon();
        this._updateEndPolygon();
    },
    _updateStartPolygon: function(){
        if(this._startPolygon != null && this._start != null && this._end != null){
            this._startPolygon.setOrigin(this._start);
            this._startPolygon.resetRotation();
            var line = new Line(this._start,this._line.getFirstControl());
            this._startPolygon.rotate(270-line.getXAxisTheta());
        }
    },
    _updateEndPolygon: function(){
        if(this._endPolygon != null && this._end != null && this._end != null){
            this._endPolygon.setOrigin(this._end);
            this._endPolygon.resetRotation();
            var line = new Line(this._line.getSecondControl() != null ? this._line.getSecondControl() : this._line.getFirstControl() ,this._end);
            this._endPolygon.rotate(90-line.getXAxisTheta());
        }
    }
});